package com.example.problem01.service;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName:MessageService
 * Package:com.example.problem01.service
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/8/13-20:47
 * @Version:v1.0
 */
@Service
@Slf4j
public class MessageService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Value("${my.exchangeName}")
    private String exchangeName;


    private static final String MESSAGE_EXCHANGE = "测试延迟队列1";
    private static final String MESSAGE_PROBLEM = "测试延迟队列2";

    public void sendMsg() {

        /*第一条消息*/
        {
            MessageProperties messageProperties = new MessageProperties();
            messageProperties.setExpiration("25000");//消息过期时间

            Message message = MessageBuilder.withBody(MESSAGE_EXCHANGE.getBytes()).andProperties(messageProperties).build();

            rabbitTemplate.convertAndSend(exchangeName, "order", message);

            log.info("消息发送完毕,发送时间为:{}", new Date());
        }
        /*第二条消息*/
        {
            MessageProperties messageProperties = new MessageProperties();
            messageProperties.setExpiration("15000");//消息过期时间

            Message message = MessageBuilder.withBody(MESSAGE_PROBLEM.getBytes()).andProperties(messageProperties).build();

            rabbitTemplate.convertAndSend(exchangeName, "order", message);

            log.info("消息发送完毕,发送时间为:{}", new Date());
        }

    }
}
